<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<script>

// async function retry (fn, tryNum = 0) {
//     tryNum--
//     console.log('retry')
//     let value
//     let promiseFn = fn()
//     new Promise((resolve, reject) => {
//         promiseFn.then((res) => {
//             value = res
//             resolve(res)
//         }).catch((err) => {
//             console.log(111111)
//             if (tryNum <= 0) {
//                 console.log(22222)
//                 reject(err)
//             } else {
//                 console.log(33333)
//                 retry(fn, tryNum)
//             }
//         })
//     })
// }


function retry (fn, maxTry = 0) {
    return new Promise((resolve, reject) => {
        fn().then((res) => {
            resolve(res)
        }).catch((err) => {
            if (maxTry > 1) {
                retry(fn, maxTry - 1)
            } else {
                reject(err)
            }
        })
    })
}


function getValue () {
    return new Promise((resolve, reject) => {
        console.log('aaaa')
        setTimeout(() => {
            console.log('我执行了一次')
            reject('haha')
        }, 1000);
    })
}

retry(getValue, 3)

</script>
</body>
</html>